package com.leinao.core;

import cn.hutool.core.collection.CollectionUtil;
import com.leinao.core.pool.DevicePointInfoPool;
import com.leinao.core.pool.TableInfoPool;
import com.leinao.domain.PointInfo;
import com.leinao.domain.TableInfo;
import com.leinao.domain.param.DeviceDataQueryParam;
import com.leinao.service.MysqlService;
import com.leinao.service.TdengingService;
import com.leinao.utils.DateUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * init database
 *
 * @author zhong.wang
 * @date 2025/1/15
 */
@Component
public class InitDataBase {

    @Resource
    private MysqlService mysqlService;

    @Resource
    private TdengingService tdengingService;

    @PostConstruct
    public void init() throws ParseException {
        this.initTableInfo();
        this.initPointInfo();
       // this.initData();
    }

    public void initTableInfo(){
        List<Long> deviceModelIds = mysqlService.getDeviceModelIdList();
        for (Long deviceModelId : deviceModelIds) {
            TableInfoPool.getInstance().registerDeviceModelId(deviceModelId);
            String superTableName = "device_model_" + deviceModelId;
            List<TableInfo> tableInfos = tdengingService.getTableInfoBySuperTable(superTableName);
            if(CollectionUtil.isNotEmpty(tableInfos)){
                TableInfoPool.getInstance().registerTableInfo(deviceModelId,tableInfos);
            }
        }
    }

    public void initPointInfo(){
        List<PointInfo> pointInfos = mysqlService.getPointInfoList();
        if(CollectionUtil.isNotEmpty(pointInfos)){
            Map<Long,PointInfo> map = pointInfos.stream().collect(Collectors.toMap(PointInfo::getId, Function.identity()));
            for (Long id : map.keySet()) {
                DevicePointInfoPool.getInstance().register(id,map.get(id));
            }
            Map<Long,List<PointInfo>> devicePointMap = pointInfos.stream().collect(Collectors.groupingBy(PointInfo::getDeviceId));
            for (Long id : devicePointMap.keySet()) {
                DevicePointInfoPool.getInstance().registerDevicePoint(id,devicePointMap.get(id));
            }
        }
    }

    public void initData(){
        Map<Long, List<TableInfo>> tableInfoPool = TableInfoPool.getInstance().getTableInfoPool();

        for(Map.Entry<Long,List<TableInfo>> entry : tableInfoPool.entrySet()){
            List<TableInfo> tableInfos = entry.getValue();
            for (TableInfo tableInfo : tableInfos) {
                System.out.println(tableInfo.getDeviceId());
                List<PointInfo> pointInfos = DevicePointInfoPool.getInstance().getByDeviceId(tableInfo.getDeviceId());
                if(CollectionUtil.isNotEmpty(pointInfos)){
                    List<Long> pointIds = pointInfos.stream().map(PointInfo::getId).collect(Collectors.toList());
                    DeviceDataQueryParam queryParam = new DeviceDataQueryParam();
                    queryParam.setPointIds(pointIds);
                    queryParam.setStartTime("2024-12-08 00:00:00");
                    queryParam.setEndTime("2024-12-09 00:00:00");
                    queryParam.setTimeGroupInterval("1d");
                    queryParam.setTableName("device_" + tableInfo.getDeviceId());
                    tdengingService.insertData(queryParam);
                }
            }
        }
    }


}
